﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Configuration.Install;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using System.Windows.Forms;

namespace MallInteractiveMapCMSCustomAction
{
    [RunInstaller(true)]
    public class MallInteractiveSetupAction:Installer
    {
        const string DATABASE_CONNECTION_KEY = "MallInteractiveMapDB";

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            // Retrieve configuration settings
            string targetSite = Context.Parameters["targetsite"];
            string targetVDir = Context.Parameters["targetvdir"];
            string targetDirectory = Context.Parameters["targetdir"];
            string sname = Context.Parameters["sname"];
            string dname = Context.Parameters["dname"];
            string uname = Context.Parameters["uname"];
            string password = Context.Parameters["password"];
            string connectionstring = string.Format("Data Source={0};Initial Catalog={1}; User ID={2}; Password={3};",
                                                    sname, dname, uname, password);
            

            if (targetSite == null)
                throw new InstallException("IIS Site Name Not Specified!");

            if (targetSite.StartsWith("/LM/"))
                targetSite = targetSite.Substring(4);

            if (connectionstring == null)
                throw new InstallException("You did not specify a database to use!");

            
            ConfigureDatabase(targetSite, targetVDir, connectionstring);
        }
        void ConfigureDatabase(string targetSite, string targetVDir, string connectionString)
        {
            try
            {
                // Retrieve "Friendly Site Name" from IIS for TargetSite
                DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/" + targetSite);
                string friendlySiteName = entry.Properties["ServerComment"].Value.ToString();

                // Open Application's Web.Config
                Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetVDir, friendlySiteName);

                // Create and add new connection string setting for web.config
                ConnectionStringSettings appDatabase = new ConnectionStringSettings();
                appDatabase.Name = DATABASE_CONNECTION_KEY;
                appDatabase.ConnectionString = connectionString;

                config.ConnectionStrings.ConnectionStrings.Clear();
                config.ConnectionStrings.ConnectionStrings.Add(appDatabase);

                // Persist web.config settings
                config.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }
}
